package opc_service

import (
	"beta/pkg/e"
	"beta/pkg/logger"
	"beta/pkg/util"
	"errors"
	"github.com/tidwall/gjson"
	"strconv"
	"time"
)

type DepositBill struct {
	AccessId string `json:"accessId"`
	CustomerID     string `json:"customerId"`
	AccountID      int    `json:"accountId"`
	SalesChannel string `json:"salesChannel"`
	CapitalType string `json:"capitalType"`
	DepositAmount  string `json:"depositAmount"`
	EffectiveStart string `json:"effectiveStart"`
	EffectiveEnd   string `json:"effectiveEnd"`
	OperatorID     string `json:"operatorId"`
	SignerID       string `json:"signerId"`
	Remark         string `json:"remark"`
	CouponRemark   string `json:"couponRemark"`
}

type InvoiceInfo struct {
	InvoiceType      string `json:"invoiceType"`
	InvoiceTitleType string `json:"invoiceTitleType"`
	Title            string `json:"title"`
}

type DeliveryInfo struct {
	ReceiverMobile string `json:"receiverMobile"`
	ReceiverEmail  string `json:"receiverEmail"`
}

type RemittancePay struct {
	RemittanceID string `json:"remittanceId"`
	Amount       int    `json:"amount"`
}

type CouponDeposit struct {
	DepositBill `json:"depositBill"`
	PaymentWay  string `json:"paymentWay"`
	RemittancePayList []RemittancePay `json:"remittancePay"`
}

type CashDeposit struct {
	DepositBill `json:"depositBill"`
	InvoiceInfo	`json:"invoiceInfo"`
	DeliveryInfo `json:"deliveryInfo"`
	PaymentWay  string `json:"paymentWay"`
	RemittancePayList []RemittancePay `json:"remittancePay"`
}


func (opc OpcService) AddDepositBill(accountId int, remittanceId string, depositType string, payFlag bool) (string, error) {

	host := opc.Hosts[appName].AppDomain
	url := host + "/opc/v1/deposit/createDepositBill"

	var depositOrder interface{}
	if depositType == "1" {
		depositOrder = opc.SetCashDeposit(accountId, remittanceId, depositType, payFlag)
	} else {
		depositOrder = opc.SetCouponDeposit(accountId, depositType)
	}

	logger.ZapLogger.Infow("【充值】", "请求参数", depositOrder, "traceId", opc.TraceId)

	cookie := util.GetCookie(util.BossCookieName, opc.Token)
	httpClient := util.NewHttpClientWithCookie(3 * time.Second, cookie).R()
	resp, respErr := httpClient.SetHeaders(e.GetHeaderMap(e.JsonHeader)).
		SetBody(depositOrder).Post(url)

	logger.ZapLogger.Infow("【充值】", "请求结果", resp, "traceId", opc.TraceId)

	if respErr != nil {
		return "", respErr
	}

	depositId := gjson.GetBytes(resp.Body(), "data.depositId").String()
	if depositId == "" {
		return "", errors.New("创建卡券失败")
	}

	return depositId, nil

}

func (opc OpcService) SetCashDeposit(accountId int, remittanceId string, depositType string, payFlag bool) CashDeposit {

	depositBill := DepositBill{
		AccessId:       "",
		CustomerID:     opc.CustomerID,
		AccountID:      accountId,
		SalesChannel:   "50",
		CapitalType:    depositType,
		DepositAmount:  opc.DepositNum,
		EffectiveStart: "",
		EffectiveEnd:   "",
		OperatorID:     e.GetOperatorId(opc.Operator),
		SignerID:       e.GetOperatorId(opc.Operator),
		Remark:         "",
		CouponRemark:   "",
	}

	cashAmount, _ := strconv.Atoi(opc.DepositNum)

	invoiceInfo := InvoiceInfo{
		InvoiceType:      "4",
		InvoiceTitleType: "2",
		Title:            opc.FullName,
	}

	deliveryInfo := DeliveryInfo{
		ReceiverMobile: opc.ReceiverMobile,
		ReceiverEmail: opc.ReceiverEmail,
	}

	pay := RemittancePay{
		RemittanceID: remittanceId,
		Amount:       cashAmount,
	}

	paymentWay := ""
	var remittancePayList []RemittancePay
	
	if payFlag == true {
		paymentWay = "1"
		remittancePayList = append(remittancePayList, pay)
	}
	
	cashDeposit := CashDeposit{
		DepositBill:       depositBill,
		InvoiceInfo:       invoiceInfo,
		DeliveryInfo:      deliveryInfo,
		PaymentWay:        paymentWay,
		RemittancePayList: remittancePayList,
	}
	return cashDeposit
}


func (opc OpcService) SetCouponDeposit(accountId int, depositType string) CouponDeposit {
	depositBill := DepositBill{
		AccessId:       "",
		CustomerID:     opc.CustomerID,
		AccountID:      accountId,
		SalesChannel:   "50",
		CapitalType:    depositType,
		DepositAmount:  opc.DepositNum,
		EffectiveStart: "",
		EffectiveEnd:   "",
		OperatorID:     e.GetOperatorId(opc.Operator),
		SignerID:       e.GetOperatorId(opc.Operator),
		Remark:         "",
		CouponRemark:   "测试卡券备注",
	}

	cashDeposit := CouponDeposit{
		DepositBill:       depositBill,
		PaymentWay:        "",
		RemittancePayList: nil,
	}

	return cashDeposit
}
